package com.fr71.project.dm.controller;

import com.fr71.common.utils.DateUtils;
import com.fr71.framework.aspectj.lang.annotation.Log;
import com.fr71.framework.aspectj.lang.enums.BusinessType;
import com.fr71.framework.web.controller.BaseController;
import com.fr71.framework.web.domain.AjaxResult;
import com.fr71.framework.web.page.TableDataInfo;
import com.fr71.common.utils.poi.ExcelUtil;
import com.fr71.project.dm.domain.PdOrder;
import com.fr71.project.dm.service.IPdOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 开发者：https://github.com/devzwy
 * 创建时间：2023-06-28
 * 类描述：订单Controller
 */
@RestController
@RequestMapping("/dm/order")
public class PdOrderController extends BaseController {
    
    @Autowired
    private IPdOrderService pdOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('dm:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(PdOrder pdOrder) {
        startPage();
        List<PdOrder> list = pdOrderService.selectPdOrderList(pdOrder);
        return getDataTable(list);
    }

    /**
     * 获取首页数据
     */
    @GetMapping("/getHomeData")
    public AjaxResult getHomeData() {
        return pdOrderService.getHomeData();
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('dm:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PdOrder pdOrder) {
        List<PdOrder> list = pdOrderService.selectPdOrderList(pdOrder);
        ExcelUtil<PdOrder> util = new ExcelUtil<PdOrder>(PdOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('dm:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(pdOrderService.selectPdOrderByOrderId(orderId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('dm:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PdOrder pdOrder) {
        return toAjax(pdOrderService.insertPdOrder(pdOrder));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('dm:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/addNew")
    public AjaxResult addNew(@RequestBody PdOrder pdOrder) {
        pdOrder.setOrderId(UUID.randomUUID().toString().replace("-", ""));
        pdOrder.setBindIp("自定义");
        pdOrder.setOrderStatus(1);
        pdOrder.setOrderType(0);
        pdOrder.setBuyType("buy");
        pdOrder.setPayTime(DateUtils.getNowDate());
        pdOrder.setPayType("alipay");
        pdOrder.setIp("自定义");
        pdOrder.setPayData("自定义");
        pdOrder.setPriceCode(106);
        pdOrder.setQq("66666666");
        pdOrder.setTradeNo("66666666");
        pdOrder.setsCode("12345678");
        return toAjax(pdOrderService.insertPdOrder(pdOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('dm:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PdOrder pdOrder) {
        return toAjax(pdOrderService.updatePdOrder(pdOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('dm:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds) {
        return toAjax(pdOrderService.deletePdOrderByOrderIds(orderIds));
    }
}